package com.android.example.AIDLClient;

import android.app.Activity;
import android.os.Bundle;
import com.android.example.AIDL.*;  
import android.content.ComponentName;  
import android.content.Context;  
import android.content.Intent;  
import android.content.ServiceConnection;  
import android.os.IBinder;  
import android.view.View;  
//import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
  
 public class AIDLClient extends Activity //implements OnClickListener  
 {  
     private IMyService myService = null;  
     private Button btnInvokeAIDLService;  
     private Button btnBindAIDLService;   
     private TextView textView;
     
     private ServiceConnection serviceConnection = new ServiceConnection()  
     {  
      @Override  
      public void onServiceConnected(ComponentName 
name, IBinder service)  
         {  
             //  获得服务对象  
             myService = IMyService.Stub.asInterface(service);  
             btnInvokeAIDLService.setEnabled(true);  
         }  
         @Override  
         public void onServiceDisconnected(ComponentName name)  
         {  
         }  
     };  
//     @Override  
//     public void onClick(View view)  
//     {  
//         switch (view.getId())  
//         {  
//             case R.id.bind_button:  
//                 //  绑定AIDL服务  
//                 bindService(new Intent("com.android.example.AIDL.IMyService"),  
//                         serviceConnection, Context.BIND_AUTO_CREATE);  
//                 break;  
//             case R.id.invoke_button:  
//                 try  
//                 {  
//                     textView.setText(myService.
//getValue());     //  调用服务端的getValue方法  
//                 }  
//                 catch (Exception e)  
//                 {  
//                 }  
//                 break;  
//         }  
//     }
     
     @Override  
     public void onCreate(Bundle savedInstanceState)  
     {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
         btnInvokeAIDLService = (Button) findViewById(R.id.invoke_button);  
         btnBindAIDLService = (Button) findViewById(R.id.bind_button);  
         btnInvokeAIDLService.setEnabled(false);  
         textView = (TextView) findViewById(R.id.textview);  
         btnInvokeAIDLService.setOnClickListener(new Button.OnClickListener() {
        	 public void onClick(View v) {
        		 try
        		 {
//        			 textView.setText(myService.getValue());
        			 String s = "";  
        			 //  调用AIDL服务中的方法  
        			 s = "Product.id = " + myService.getProduct().getId() + "\n";  
        			 s += "Product.name = " + myService.getProduct().getName() + "\n";  
        			 s += "Product.price = " + myService.getProduct().getPrice() + "\n";              
        			 s += myService.getMap("China",myService.getProduct()).toString();  
        			 textView.setText(s);  
        		 }
        		 catch (Exception e)
        		 {
        			 
        		 }
        		 
        	 }
         });  
         btnBindAIDLService.setOnClickListener(new Button.OnClickListener() {
        	 public void onClick(View v)
        	 {
        		 bindService(new Intent("com.android.example.AIDL.IMyService"),  
                         serviceConnection, Context.BIND_AUTO_CREATE);  
        	 }
         });
         
     }
     
     
 } 